//
//  YSEmotionView.swift
//  新浪浪
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

let YSEmotionViewReuseIdentifier = "YSEmotionViewReuseIdentifier"

class YSEmotionView: UIView,UICollectionViewDataSource,YSaEmotionToolBarViewDelegate {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        setupCollectiongView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //添加
        addSubview(collectionView)
        addSubview(toolbar)
        //设置约束
        collectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(toolbar.snp_top)
            make.right.equalTo(self.snp_right)
        }
        toolbar.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(self.snp_bottom)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(40)
        }
    }
    
    private func setupCollectiongView(){
        collectionView.backgroundColor = UIColor.purpleColor()
        collectionView.pagingEnabled = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.dataSource = self
        collectionView.registerClass(YSEmotionCollectionCell.self, forCellWithReuseIdentifier: YSEmotionViewReuseIdentifier)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = .Horizontal
        flowLayout.itemSize = CGSizeMake(UIScreen.mainScreen().bounds.width, collectionView.bounds.height)
    
    }
    
    
    // 懒加载 上面的collectionView和下面的toobarView
    private lazy var flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    private lazy var collectionView : UICollectionView = UICollectionView (frame: CGRectZero, collectionViewLayout: self.flowLayout)
    private lazy var toolbar : YSaEmotionToolBarView = {
        let tool = YSaEmotionToolBarView()
        tool.delegate = self
        return tool
        }()
}


extension YSEmotionView{
    func didEmotionToolBarItemClick(type: YSaEmotionToolBarType) {
        switch type {
        case .Recent:
            let index = NSIndexPath (forItem: 0, inSection: 0)
            collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
            
        case .Default:
            let index = NSIndexPath (forItem: 0, inSection: 1)
            collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
            
        case .Emoji:
            let index = NSIndexPath (forItem: 0, inSection: 2)
            collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
            
        case .Lxh:
            let index = NSIndexPath (forItem: 0, inSection: 3)
            collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
//        default: break
        }
    }
}


/********************+++++++++++*********
数据源方法
***************+++++++++++*********************/
extension YSEmotionView {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return YSEmotionManager.getAllEmoticon().count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return YSEmotionManager.getAllEmoticon()[section].count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YSEmotionViewReuseIdentifier, forIndexPath: indexPath) as! YSEmotionCollectionCell
//        cell.lab.text = "这是第\(indexPath.section)组,第\(indexPath.row)个item"
//        cell.emoticons = YSEmotionManager.getAllEmoticon()[indexPath.section][indexPath.item]
            cell.emoticons = YSEmotionManager.getAllEmoticon()[indexPath.section][indexPath.item]
        
        return cell
    }
}


/********************+++++++++++*********
自定义cell
***************+++++++++++*********************/
class YSEmotionCollectionCell:UICollectionViewCell {
    
    var emoticons: [YSEmotion]?{
        didSet{
            for (_,button) in reuserButton.enumerate(){
//
                button.hidden = true
            }
            for (index,emoticon) in emoticons!.enumerate() {
                let button = reuserButton[index]
                button.emotion = emoticon    
            }
            
        }
    }
        
    private lazy var reuserButton: [YSEmotionButton] = [YSEmotionButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    private func setupUI(){
        
        addEmotion()
        contentView.addSubview(lab)
        
        lab.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView.snp_center)/********************+++++++++++*********
            BUG  contentView.center != content.snp_center
            ***************+++++++++++*********************/
        }
        
    }
    
    private func addEmotion(){
        let W = frame.size.width/7
        let H = frame.height / 3
        
        
        for i in 0 ..< 20 {
            let button = YSEmotionButton (type: .Custom)
            let x = i % 7
            let y = i / 7
            button.frame = CGRectMake(CGFloat(x)*W, CGFloat(y)*H, W, H)
//            button.titleLabel?.text = "\(i)"
            button.setTitle("\(i)", forState: UIControlState.Normal)
            button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
            button.addTarget(self, action: "emotionClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
//            button.emotion =
            reuserButton.append(button)
            contentView.addSubview(button)
        }
        
        contentView.addSubview(deletButton)
        deletButton.frame = CGRectMake(6*W, 2*H, W, H)
    }
    //创建一个通知中心
    @objc private func emotionClick(button:YSEmotionButton){
        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEmoticonTransform, object: nil, userInfo:["emoticon": button.emotion!])
        
        YSEmotionManager.addRecentEmoticon(button.emotion)
        
    }
    
    //懒加载
    private lazy var lab : UILabel = {
       let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(16)
        
        return label
    }()
    private lazy var deletButton : UIButton = {
        let btn = UIButton(type: .Custom)
        btn.setImage(UIImage (named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage (named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
        }()
    
}







